<template>
  <div style="display: flex; height: 100%">
    <SideBar class="fl" :groupSettings="groupSettings"></SideBar>
    <div class="spare-parts fr">
      <keep-alive exclude="QuickSearch">
        <router-view></router-view>
      </keep-alive>
    </div>
  </div>
</template>

<script lang="ts">
import SideBar from '@/common/views/sideBar/index.vue'
import { Component, Vue } from 'vue-property-decorator'
import { commonAction } from '@/utils/common'
import { mutations } from './store/spareData'
@Component({
  name: 'SpareParts',
  components: { SideBar }
})
export default class Spare extends Vue {
  [x: string]: any
  private groupSettings = {
    spare: {
      title: '备件管理'
    }
  }

  private getAllTypeOptions () { // 获取全部类型数据（包含删除）
    commonAction({
      url: '/server-work/action/spareConfig/listAll',
      method: 'get'
    }).then(res => {
      if (res.success) {
        mutations.setStateData('obj', 'allSpareTypeObj', this.formateSpareType([], res.data.spareTypeList))
        mutations.setStateData('obj', 'allSpareOutStockTypeObj', res.data.spareOutStockTypeList)
        mutations.setStateData('obj', 'allSpareInStockTypeObj', res.data.spareInStockTypeList)
        mutations.setStateData('obj', 'allUnitObj', res.data.unitConfig)
      }
    })
  }

  private formateSpareType (result: any[], val: any[]) {
    val.forEach(item => {
      result.push(item)
      if (Array.isArray(item.children) && item.children.length) {
        this.formateSpareType(result, item.children)
      }
    })
    return result
  }

  private getTypeOptions () { // 获取类型数据（不包含删除的数据）
    commonAction({
      url: '/server-work/action/spareConfig/list',
      method: 'get'
    }).then(res => {
      if (res.success) {
        mutations.setRequestData('type', true)
        mutations.setStateData('arr', 'spareTypeList', res.data.spareTypeList)
        mutations.setStateData('arr', 'spareOutStockTypeList', res.data.spareOutStockTypeList)
        mutations.setStateData('arr', 'spareInStockTypeList', res.data.spareInStockTypeList)
        mutations.setStateData('arr', 'unitList', res.data.unitConfig)
      }
    })
  }

  private getAllHouseOptions () { // 获取仓库数据（包含删除）
    commonAction({
      url: '/server-work/action/wareHouse/query',
      method: 'post',
      data: { isIncludeDel: true }
    }).then(res => {
      if (res.success) {
        mutations.setStateData('obj', 'allHouseObj', res.data.wareHouseItemList)
      }
    })
  }

  private getHouseOptions () { // 获取仓库数据（不包含删除的数据）
    commonAction({
      url: '/server-work/action/wareHouse/query',
      method: 'post',
      data: { isIncludeDel: false }
    }).then(res => {
      if (res.success) {
        mutations.setRequestData('house', true)
        mutations.setStateData('arr', 'houseList', res.data.wareHouseItemList)
      }
    })
  }

  private getAllSpare () { // 获取备件品类（包含删除）
    commonAction({
      url: '/server-work/action/spare/query',
      method: 'post',
      data: { isIncludeDel: true }
    }).then(res => {
      if (res.success) {
        mutations.setStateData('obj', 'allCommodityCodeObj', res.data.spareItemList)
      }
    })
  }

  // private activated () {
  //   this.getTypeOptions()
  //   this.getHouseOptions()
  // }

  private created () {
    this.getAllTypeOptions()
    this.getAllHouseOptions()
    this.getAllSpare()
  }
}
</script>

<style lang="stylus" scoped>
.spare-parts
  width calc(100vw - 262px)
  height 100%
  flex 1
  padding-left 16px
.navMenuState .spare-parts
  width calc(100vw - 314px)
</style>
